<template>
  <view v-if="loaded" class="page">
    <view class="bar">
      <view class="top-search-bar">
        <image class="search-icon" src="/static/icon/search.png"></image>
        <!-- v-model="queryParams.couponName" -->
        <input
          class="input"
          v-model="testValue"
          @input="inputHandler"
          placeholder="大家都在搜"
          placeholder-class="pla"
        />
      </view>
    </view>

    <view class="brand_container">
      <view class="item" v-for="(item, index) in brandData" @click="topClick(index)">
        <image class="img" :src="IMG_URL + item.brandLogoImage" mode="aspectFill"></image>
        <view class="title" :class="selectTopIndex == index ? 'select' : ''">{{ item.brandName }}</view>
      </view>
    </view>
    <view class="content">
      <view class="left">
        <view
          class="item"
          :class="selectLeftIndex == index ? 'active' : ''"
          @click="leftClick(index)"
          v-for="(item, index) in shopData"
        >
          {{ item.shopName }}
        </view>
      </view>
      <view class="right">
        <div class="bag_box" v-for="item in fixData">
          <div class="title">{{ item.title }}（共{{ JSON.stringify(item.list.length) }}张）</div>
          <div class="goods">
            <div class="goods_item" v-for="(i, index) in item.list">
              <div class="img_box" @click="handleGoodsClick(i)">
                <div class="select_box" v-if="mode == 'relate'">
                  <view class="border"></view>
                  <image v-show="checkSelect(i)" class="spot" src="/static/icon/select.png" mode="aspectFit" />
                </div>

                <image class="img" :src="IMG_URL + i.goodsImage" mode="aspectFill"></image>
                <div class="view">
                  <image class="icon" src="/static/icon/view.png" mode="widthFix"></image>
                  <span class="num">浏览量 {{ i.checkNum || 0 }}</span>
                </div>
              </div>

              <div class="t">{{ i.goodsName }}</div>
              <div class="sale">销量：{{ i.saleCount || 0 }}</div>
            </div>
          </div>
        </div>
      </view>
    </view>
  </view>

  <view v-else class="empty_page">
    <div class="tips">暂无数据</div>
  </view>
</template>

<script setup>
  import { ref, reactive, computed } from 'vue';
  import { onShow, onLoad } from '@dcloudio/uni-app';
  import { queryAllBrand, queryAllShop, queryGoodsById } from '../../apis/pages';
  import { IMG_URL } from '../../config/config';
  import { useFormStore } from '../../store/form';

  const formStore = useFormStore();

  const selectTopIndex = ref(0); // 默认选中第一个品牌
  const selectLeftIndex = ref(-1); // 默认选中第一个商户

  const mode = ref('');

  const couponDict = {
    1: '自制券',
    2: '线上券',
    3: '第三方接口券',
    4: '实物商品',
  };

  const fixData = computed(() => {
    if (goodsData.value) {
      let r = Object.keys(goodsData.value).map((key) => {
        return {
          title: couponDict[key],
          list: [...goodsData.value[key]],
        };
      });
      console.log(r, 'r <<<');
      return r;
    }
  });

  const inputTimer = ref(-1);

  // 搜索参数
  const queryParams = reactive({
    // couponName: '',
    brandId: '', //品牌id
    shopId: '', //店铺id
  });

  const loaded = ref(false);
  // 品牌列表
  const brandData = ref([]);

  // 店铺列表
  const shopData = ref([]);

  const goodsData = ref();

  onLoad((options) => {
    if (options.mode) {
      mode.value = options.mode;
    }

    console.log(mode.value, 'mode >>>>');
    console.log('onLoad');
    // 获取所有品牌列表
    getAllBrands();
  });

  function getAllBrands() {
    queryAllBrand({
      merchantId: uni.getStorageSync('merchantId'),
    }).then((res) => {
      console.log(res, 'brands res >>>>>>');
      if (!res.data || res.data.length === 0) {
        loaded.value = false;
      } else {
        brandData.value = res.data;
        loaded.value = true;
        // 查询商品
        queryParams.brandId = brandData.value[selectTopIndex.value].id;

        // 查询店铺
        queryAllShops();
      }
    });
  }

  // 查询店铺
  function queryAllShops() {
    queryAllShop({
      merchantId: uni.getStorageSync('merchantId'),
      ...queryParams,
    }).then((res) => {
      shopData.value = res.data;
      console.log(res, 'res >>>>>>');
      if (res.data.length) {
        // 查询商品;
        queryGoodsData();
      }
    });
  }

  function queryGoodsData() {
    queryGoodsById({
      merchantId: uni.getStorageSync('merchantId'),
      ...queryParams,
    }).then((res) => {
      console.log(res, 'Goods res >>>>>>');
      goodsData.value = res.data;
    });
  }

  const testValue = ref();

  // 搜索框输入
  function inputHandler(e) {
    // todo 搜索功能
    return;

    queryParams.couponName = e.detail.value;
    // 限制300ms后请求
    clearTimeout(inputTimer.value);
    inputTimer.value = setTimeout(() => {
      // todo 更新商品列表
      // queryCouponByIdList();
    }, 500);
  }

  function topClick(index) {
    if (selectTopIndex.value === index) return;
    selectTopIndex.value = index;
    queryParams.brandId = brandData.value[index].id;
    queryParams.shopId = '';
    selectLeftIndex.value = -1;

    queryAllShops();
    queryGoodsData();
  }

  function leftClick(index) {
    if (selectLeftIndex.value === index) return;
    selectLeftIndex.value = index;
    console.log(index, 'leftClick index >>>>>>');
    console.log(shopData.value, 'shopData >>>>>>');
    queryParams.shopId = shopData.value[index].id;
    // queryAllShops();
    queryGoodsData();
  }

  function handleGoodsClick(item) {
    if (mode.value == 'relate') {
      if (formStore.relateGoodsId.includes(item.id)) {
        let index = formStore.relateGoodsId.findIndex((i) => i == item.id);
        formStore.relateGoodsId.splice(index, 1);
      } else {
        formStore.relateGoodsId.push(item.id);
      }
    }
  }

  function checkSelect(item) {
    return formStore.relateGoodsId.includes(item.id);
  }
</script>

<style scoped lang="scss">
  .page {
    display: flex;
    flex-direction: column;
    width: 100vw;
    height: 100vh;
    box-sizing: border-box;
    font-size: 28rpx;
    background-color: #f5f5f5;
  }

  .empty_page {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100vw;
    height: 100vh;
    .tips {
      font-size: 30rpx;
      color: #333;
    }
  }

  .bar {
    padding: 20rpx;
    .top-search-bar {
      display: flex;
      align-items: center;
      width: 100%;
      height: 70rpx;
      background-color: #fff;
      border-radius: 35rpx;
      box-sizing: border-box;
      font-size: 24rpx;
      color: #333;
      padding: 0 22rpx;
      box-sizing: border-box;

      .search-icon {
        width: 44rpx;
        height: 44rpx;
        margin-right: 10rpx;
      }

      .input {
        width: 100%;
        height: 100%;
        font-size: 24rpx;
        box-sizing: border-box;
      }
    }
  }

  .brand_container {
    white-space: nowrap;
    overflow-x: scroll;
    padding: 10rpx 20rpx;
    margin-bottom: 15rpx;

    -ms-overflow-style: none; /* IE 和 Edge */
    scrollbar-width: none; /* Firefox */

    &::-webkit-scrollbar {
      width: 0;
      height: 0;
      display: none;
    }
    .item {
      display: inline-flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      margin-right: 14rpx;
      .img {
        width: 125rpx;
        height: 94rpx;
        border-radius: 10rpx;
        margin-bottom: 20rpx;
        background-color: #ccc;
      }

      .title {
        max-width: 125rpx;
        font-size: 22rpx;
        color: #1e2737;
        /* 显示省略号 */
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;

        &.select {
          color: #3b77fa;
        }
      }
    }
  }

  .content {
    display: flex;
    flex: 1;
    background-color: #fff;
    border-top-left-radius: 60rpx;
    border-top-right-radius: 60rpx;
    overflow: hidden;
    width: 100%;
  }

  .left {
    flex: 16;
    background-color: #f0f0f0;
    overflow-y: scroll;
    padding-bottom: 40rpx;

    .item {
      height: 82rpx;
      line-height: 82rpx;
      text-align: center;
      font-size: 24rpx;
      color: #1e2737;
      /* 显示省略号 */
      overflow: hidden;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 1;

      &.active {
        background-color: #fff;
      }
    }
  }

  .right {
    flex: 59;
    overflow-y: scroll;
    padding-bottom: 40rpx;
    .bag_box {
      width: 100%;
      padding: 0 24rpx;
      padding-top: 40rpx;
      box-sizing: border-box;
      .title {
        font-weight: 500;
        font-size: 26rpx;
        color: #1e2737;
        margin-bottom: 30rpx;
        /* 显示省略号 */
        overflow: hidden;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1;
      }
      .goods {
        display: flex;
        flex-wrap: wrap;
        // justify-content: space-between;
        .goods_item {
          display: inline-flex;
          flex-direction: column;
          margin-right: 20rpx;
          margin-bottom: 20rpx;

          &:nth-child(3n) {
            margin-right: 0;
          }

          .img_box {
            position: relative;
            margin-bottom: 4rpx;

            .img {
              width: 160rpx;
              height: 120rpx;
              border-radius: 16rpx;
              vertical-align: middle;
              background-color: #ccc;
            }

            .view {
              display: flex;
              align-items: center;
              position: absolute;
              left: 0;
              bottom: 0;
              width: 100%;
              height: 34rpx;
              background-color: rgba(0, 0, 0, 0.3);
              border-bottom-left-radius: 16rpx;
              border-bottom-right-radius: 16rpx;

              .icon {
                width: 22rpx;
                height: 16rpx;
                margin-right: 4rpx;
              }

              .num {
                font-size: 20rpx;
                color: #ffffff;
              }
            }
          }

          .t {
            max-width: 160rpx;
            font-weight: 500;
            font-size: 20rpx;
            color: #1e2737;
            margin-bottom: 4rpx;
            /* 显示省略号 */
            overflow: hidden;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 1;
          }

          .sale {
            font-size: 20rpx;
            color: #1e2737;
          }
        }
      }
    }
  }

  .pla {
    color: #999;
    font-size: 24rpx;
  }

  .select_box {
    position: absolute;
    right: 10rpx;
    top: 10rpx;
    width: 28rpx;
    height: 28rpx;

    .border {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      border: 1px solid #474747;
      border-radius: 50%;
    }

    .spot {
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%, -50%);
      width: 28rpx;
      height: 28rpx;
      vertical-align: middle;
    }
  }
</style>
